﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using DDay.iCal.Components;

namespace InquiryRequestActivities
{
    public partial class CalculateRatesForStay : SequenceActivity
    {

        private void CalculateRatesActivity_ExecuteCode(object sender, EventArgs e)
        {
            InquiryState.inquiry.occurences = InquiryState.inquiry.bookings.GetOccurrences(InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(-30), InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(30));
            InquiryState.inquiry.isBooked = false;
            for (int i = 0; i < InquiryState.inquiry.parsedRequest.NumNights; i++)
            {

                Decimal x = InquiryState.inquiry.rateTable.GetRateForDate(InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(i));

                if (!CheckAvailability(InquiryState.inquiry.occurences, InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(i)))
                {
                    InquiryState.inquiry.isBooked = true;
                    InquiryState.inquiry.bookedMessages.AppendFormat("{0} is booked - rate ${1}.00\r\n", InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(i).ToShortDateString(), x);
                }
                else
                {
                    InquiryState.inquiry.availableMessages.AppendFormat("{0} available - rate ${1}.00\r\n", InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(i).ToShortDateString(), x);
                }

                InquiryState.inquiry.totalRate += (float)x;
            }

            if (InquiryState.inquiry.isBooked)
            {
                for (DateTime st = InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(-InquiryState.inquiry.parsedRequest.NumNights);
                     st <= InquiryState.inquiry.parsedRequest.ArrivalDate.AddDays(InquiryState.inquiry.parsedRequest.NumNights * 2);
                     st = st.AddDays(1))
                {
                    Decimal x = InquiryState.inquiry.rateTable.GetRateForDate(st);

                    if (!CheckAvailability(InquiryState.inquiry.occurences, st))
                    {
                        InquiryState.inquiry.availability.AppendFormat("{0} is booked - rate ${1}.00\r\n", st.ToShortDateString(), x);
                    }
                    else
                    {
                        InquiryState.inquiry.availability.AppendFormat("{0} available - rate ${1}.00\r\n", st.ToShortDateString(), x);
                    }

                }
            }

            InquiryState.inquiry.taxAmount = (double)InquiryState.inquiry.totalRate * InquiryState.inquiry.taxPCT;
        }

        private bool CheckAvailability(List<DDay.iCal.Components.Occurrence> occList, DateTime dateTime)
        {
            DateTime chkDtTm = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 19, 0, 0);
            foreach (Occurrence occ in occList)
            {
                if (occ.Period.StartTime <= chkDtTm && occ.Period.EndTime >= chkDtTm)
                {
                    return false;
                }

            }
            return true;
        }

    }
}
